#include "ScoreLayer.h"

ScoreLayer::ScoreLayer()
: pDistanceLabel(NULL)
, pPropCountLabel(NULL)
{
}

ScoreLayer::~ScoreLayer()
{
}

bool ScoreLayer::init()
{
  do
  {
    CC_BREAK_IF(!CCLayer::init());

    CCSize winSize = CCDirector::sharedDirector()->getWinSize();

    pDistanceLabel = CCLabelTTF::create();
    CC_BREAK_IF(!pDistanceLabel);
    pDistanceLabel->setHorizontalAlignment(kCCTextAlignmentLeft);
    pDistanceLabel->setAnchorPoint(ccp(0, 0.5));
    pDistanceLabel->setFontSize(32);
    pDistanceLabel->setPosition(ccp(winSize.width * 0.025, winSize.height * 0.95));
    addChild(pDistanceLabel);

    pPropCountLabel = CCLabelTTF::create();
    CC_BREAK_IF(!pPropCountLabel);
    pPropCountLabel->setHorizontalAlignment(kCCTextAlignmentLeft);
    pPropCountLabel->setAnchorPoint(ccp(0, 0.5));
    pPropCountLabel->setFontSize(32);
    pPropCountLabel->setPosition(ccp(winSize.width * 0.025, winSize.height * 0.85));
    addChild(pPropCountLabel);
    return true;
  } while(0);
  return false;
}

ScoreLayer * ScoreLayer::create()
{
  ScoreLayer * pLayer = new ScoreLayer();
  if (pLayer && pLayer->init())
  {
    pLayer->autorelease();
    return pLayer;
  }
  else
  {
    CC_SAFE_DELETE(pLayer);
    return NULL;
  }
}

void ScoreLayer::setDistance(int distance)
{
  char text[50];
  sprintf(text, "dis:%dm", distance);
  pDistanceLabel->setString(text);
}

void ScoreLayer::setPropCount(int count)
{
  char text[50];
  sprintf(text, "prop:%d", count);
  pPropCountLabel->setString(text);
}

void ScoreLayer::refreshScore(ScoreType type, int value)
{
  switch(type)
  {
    case DISTANCE:
      setDistance(value);
      break;
    case PROP_COUNT:
      setPropCount(value);
      break;
    default:
      break;
  }
}

void ScoreLayer::restart()
{
  setDistance(0);
  setPropCount(0);
}
